<?php

namespace App\Store\Models\Wxapp\Gift;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Member\Member;

class WxappGiftExchangelog extends Model
{
    protected $table = 'site_wxapp_gift_exchangelog';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['store_info','mem_info'])->where('cp_id',$cp_id)->orderBy('create_at', 'desc');

        $mem_id = intval($request->mem_id);
        if ($mem_id>0) {
            $query->where('mem_id', $mem_id);

            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);

        }


        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if($store_ids){//权限控制
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }
        $ids=$request->ids;
        $ids=is_array($ids)?$ids:(is_array(json_decode($ids,true))?json_decode($ids,true):[]);
        if(count($ids)>0){
            $query->whereIn('id',$ids);
        }

        $store_id = intval($request->store_id);
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }



        $gift_name = $request->gift_name;
        if ($gift_name != '') {
            $query->where('gift_name', 'like', '%' . $gift_name . '%');
        }


        $real_name = $request->real_name;
        if ($real_name != '') {
            $ids=Member::where('real_name', 'like', '%' . $real_name . '%')->where('is_del',1)->where('cp_id',$cp_id)->pluck('id');
            $query->whereIn('mem_id',$ids);
        }

        $mobile = $request->mobile;
        if ($mobile != '') {
            $ids=Member::where('mobile', 'like', '%' . $mobile . '%')->where('cp_id',$cp_id)->where('is_del',1)->pluck('id');
            $query->whereIn('mem_id',$ids);
        }
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }


        $isgetall = intval($request->isgetall) ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public static function getListByMem(Request $request,$cp_id,$mem_id){

        $query = self::where('cp_id',$cp_id)->where('mem_id',$mem_id)->orderBy('create_at', 'desc');
        $exchange_status = intval($request->exchange_status);
        if ($exchange_status>0) {
            $query->where('exchange_status', $exchange_status);
        }
        $limit = $request->limit ?? 10;
        $list = $query->paginate($limit)->toArray();
        return Common::paginateDataToArray($list);
    }


    public function store_info(){
        return $this->hasOne(SiteStore::class,'id','store_id')->select(['id','store_name']);
    }

    public function mem_info(){
        return $this->hasOne(Member::class, 'id', 'mem_id')->where('is_del',1);
    }
}
